package com.sogoodsoft.web.crm;

import java.util.Date;

import org.springframework.beans.factory.annotation.Autowired;
import org.springside.modules.orm.PropertyFilter;

import com.sogoodsoft.entity.account.User;
import com.sogoodsoft.entity.app.SysClass;
import com.sogoodsoft.entity.crm.Smstask;
import com.sogoodsoft.service.account.AccountManager;
import com.sogoodsoft.service.app.SysClassService;
import com.sogoodsoft.service.common.AbstractService;
import com.sogoodsoft.service.crm.SmstaskService;
import com.sogoodsoft.util.InfosysSecurityUtil;
import com.sogoodsoft.util.InfosysUtils;
import com.sogoodsoft.web.BaseAction;

public class SmstaskAction extends BaseAction<Smstask, Long> {

	/**
     * 
     */
    private static final long serialVersionUID = 1L;

    @Autowired
	private SmstaskService service;
	
	@Autowired
	private AccountManager accountManager;
	
	@Autowired
	private SysClassService sysSer;
	
	/**读取页面字符串计划时间**/
	private String strplantime;
	
	private String action;
	private boolean editable = true;
	
	@SuppressWarnings("rawtypes")
    @Override
	protected AbstractService getService() {
		return service;
	}

	protected void onSave(){
		//设置计划发送时间
		Date datetime = InfosysUtils.castDatetime(strplantime+":00");
		getEntity().setPlantime(datetime);
		
		//设置更新时间
		getEntity().setUpdatedate(new Date());
		
		//设置用户
		Long userid = getEntity().getUserid();
		if(userid!=null){
			User user = accountManager.getUser(getEntity().getUserid());
			getEntity().setUserid(user.getId());
			entity.setUsername(user.getName());
		}
		
		//设置类型
		SysClass type = sysSer.getByCode(Smstask.SEND_TYPE_CODE.SELF.getCODE());
		getEntity().setType(type);
		
		//设置状态
		SysClass status = sysSer.getByCode(Smstask.SEND_STATUS_CODE.WAIT.getCODE());
		getEntity().setStatus(status);		
		
	}
	/* 
	 * 非管理员，只能查询自己的短信任务
	 */
	@Override
	protected void fixFilter() {
		if(!InfosysSecurityUtil.isAdmin()){
			PropertyFilter prop = new PropertyFilter("EQL_userid", String.valueOf(InfosysSecurityUtil.getCurrentUser().getId()));
			filters.add(prop);
		}
	}
	public String input() throws Exception{
		if("edit".equals(action)){			
			//如果有短信已发送，则禁止修改
			editable=service.editable(entity);	

		}
		return INPUT;		
	}

	public String getStrplantime() {
		return strplantime;
	}

	public void setStrplantime(String strplantime) {
		this.strplantime = strplantime;
	}

	public String getAction() {
		return action;
	}

	public void setAction(String action) {
		this.action = action;
	}

	public boolean isEditable() {
		return editable;
	}

	public void setEditable(boolean editable) {
		this.editable = editable;
	}
	
	
}
